package ui;

import dao.StudentDao;
import model.Student;
import util.AccountMemoryUtil;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class LoginFrame extends JFrame {

    private String generatedCode;

    public LoginFrame() {
        setTitle("学生登录 - 奖学金系统");
        setSize(500, 350);//初始窗口
        setLocationRelativeTo(null);//让窗口在屏幕中央显示
        setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口关闭时的默认操
        setLayout(null);//在容器（比如 JFrame 或 JPanel）上关闭布局管理器
        getContentPane().setBackground(new Color(255, 240, 250));

        JLabel titleLabel = new JLabel("学生登录");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 26));
        titleLabel.setForeground(new Color(220, 20, 100));
        titleLabel.setBounds(180, 20, 200, 30);
        add(titleLabel);

        JPanel formPanel = new JPanel();
        formPanel.setLayout(null);
        formPanel.setBounds(60, 70, 380, 200);
        formPanel.setBackground(Color.WHITE);
        formPanel.setBorder(BorderFactory.createLineBorder(new Color(255, 182, 193), 2));
        add(formPanel);

        JLabel idLabel = new JLabel("学号:");
        idLabel.setBounds(30, 20, 60, 25);
        JTextField idField = new JTextField();
        idField.setBounds(100, 20, 200, 25);

        JLabel pwdLabel = new JLabel("密码:");
        pwdLabel.setBounds(30, 60, 60, 25);
        JPasswordField pwdField = new JPasswordField();
        pwdField.setBounds(100, 60, 200, 25);

        JLabel codeLabel = new JLabel("验证码:");
        codeLabel.setBounds(30, 100, 60, 25);
        JTextField codeField = new JTextField();
        codeField.setBounds(100, 100, 100, 25);

        JLabel codeDisplay = new JLabel();
        codeDisplay.setFont(new Font("Consolas", Font.BOLD, 18));
        codeDisplay.setForeground(new Color(255, 105, 180));
        codeDisplay.setBounds(210, 100, 60, 25);
        codeDisplay.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        JButton loginBtn = new JButton("登录");
        JButton registerBtn = new JButton("注册");
        loginBtn.setBounds(100, 140, 80, 30);
        registerBtn.setBounds(200, 140, 80, 30);

        stylePinkButton(loginBtn);
        stylePinkButton(registerBtn);

        formPanel.add(idLabel);
        formPanel.add(idField);
        formPanel.add(pwdLabel);
        formPanel.add(pwdField);
        formPanel.add(codeLabel);
        formPanel.add(codeField);
        formPanel.add(codeDisplay);
        formPanel.add(loginBtn);
        formPanel.add(registerBtn);

        // 自动填充账号密码
        String[] remembered = AccountMemoryUtil.loadAccount("student");
        idField.setText(remembered[0]);
        pwdField.setText(remembered[1]);

        // 生成初始验证码
        generatedCode = generateCode();
        codeDisplay.setText(generatedCode);

        codeDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
                generatedCode = generateCode();
                codeDisplay.setText(generatedCode);
            }
        });

        // 登录按钮事件
        loginBtn.addActionListener(e -> {
            String id = idField.getText().trim();
            String pwd = new String(pwdField.getPassword()).trim();
            String inputCode = codeField.getText().trim();

            if (!inputCode.equals(generatedCode)) {
                JOptionPane.showMessageDialog(this, "验证码错误", "错误", JOptionPane.ERROR_MESSAGE);
                generatedCode = generateCode();
                codeDisplay.setText(generatedCode);
                return;
            }

            StudentDao dao = new StudentDao();
            Student stu = dao.login(id, pwd);
            if (stu != null) {
                // 登录成功后保存账号密码
                AccountMemoryUtil.saveAccount(id, pwd, "student");

                JOptionPane.showMessageDialog(this, "登录成功，欢迎 " + stu.getName());
                this.setVisible(false);//前窗口或当前组件设置为不可见
                new StudentMainFrame(stu).setVisible(true);
            } else {
                JOptionPane.showMessageDialog(this, "学号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        // 注册按钮事件
        registerBtn.addActionListener(e -> {
            new RegisterFrame();
            dispose();
        });
        setVisible(true);
    }

    // 生成4位随机验证码
    private String generateCode() {
        Random r = new Random();
        return String.format("%04d", r.nextInt(10000));
    }

    // 粉色按钮样式
    private void stylePinkButton(JButton btn) {
        btn.setBackground(new Color(255, 105, 180));
        btn.setForeground(Color.WHITE);
        btn.setFocusPainted(false);
        btn.setBorder(BorderFactory.createLineBorder(Color.WHITE));
    }


}
